#include "t_box.h"

T_Box_Thread::T_Box_Thread(QObject *parent)
    : QThread(parent)
{
    this->p_reset_status();
}
void T_Box_Thread::start()
{
    this->p_reset_status();
    QThread::start();
    this->p_wait_ready();
}
void T_Box_Thread::finish()
{
    QThread::quit();
    f_closing() = true;
    this->p_wait_closing();
    this->p_reset_status();
}
bool &T_Box_Thread::f_ready()
{
    return m_ready;
}
bool &T_Box_Thread::f_closing()
{
    return m_closing;
}
void T_Box_Thread::p_reset_status()
{
    f_ready() = false;
    f_closing() = false;
}
void T_Box_Thread::p_wait_ready()
{
    while(!f_ready())
    {
        if(qApp->thread() == QThread::currentThread())
        {
            qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
        }
        else
        {
            msleep(1);
        }
    }
}
void T_Box_Thread::p_wait_closing()
{
    if(qApp->thread() != QThread::currentThread())
    {
        this->wait();
        return;
    }
    while(this->isRunning())
    {
        qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
    }
}
